﻿using System;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;
using System.Collections.Generic;

namespace HealthCommunity.Controls
{
	public partial class UCSurveyList : System.Web.UI.UserControl
	{
		private ISurveyProvider _SurveyProvider;

		private IList<Survey> _DataSource;
		private int _TotalRecords;

		public IList<Survey> DataSourse
		{
			get
			{
				var pageIndex = this.PageIndex;
				var totalRecords = 0;
				var pageSize = this.PageSize;

				if(_SurveyProvider == null)
				{
					_SurveyProvider = new SurveyProvider();
				}

				var user = SiteContext.Current.CurrentUser;

				if(user != null && user.Role.RoleName.ToLower().Equals("admin"))
				{
					var drugStore = SiteContext.Current.CurrentDrugStore;

					if(drugStore != null)
					{
						_DataSource = _SurveyProvider.GetAllSurvey(drugStore.DrugStoreID, pageIndex, pageSize, out totalRecords);
					}
				}
				else
				{
					if(user != null && user.DrugStoreID.GetValueOrDefault(0) > 0)
					{
						_DataSource = _SurveyProvider.GetAllSurvey(user.UserID, user.DrugStoreID.Value, pageIndex, pageSize, out totalRecords);
					}
				}

				this.TotalRecords = totalRecords;

				return _DataSource;
			}
		}

		public int PageIndex
		{
			get
			{
				if(ViewState["PageIndex"] == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(ViewState["PageIndex"].ToString());
				}
			}
			set
			{
				ViewState["PageIndex"] = value;
			}
		}

		public int TotalRecords
		{
			get
			{
				return _TotalRecords;
			}
			set
			{
				_TotalRecords = value;
			}
		}

		public int TotalPages
		{
			get
			{
				var data = _DataSource == null ? this.DataSourse : _DataSource;

				if(data == null)
				{
					return 0;
				}

				int totalRecords = TotalRecords;

				int totalPages = 0;

				if(totalRecords <= PageSize)
				{
					totalPages = 1;
				}

				if(totalRecords % PageSize == 0)
				{
					totalPages = totalRecords / PageSize;
				}

				if(totalRecords % PageSize > 0)
				{
					totalPages = totalRecords / PageSize + 1;
				}

				return totalPages;
			}
		}

		public int PageSize
		{
			get
			{
				return int.Parse(ddlPageSize.SelectedValue);
			}
		}

		protected void LBPageIndex_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			switch(e.CommandName)
			{
				case "PageIndex":
					{
						LinkButton theLink = sender as LinkButton;

						int index = int.Parse(theLink.Text);

						PageIndex = index - 1;

						//TotalRecords = DataSource.Count();

						BindSurvey();
					}
					break;
			}
		}

		protected void lbtnFirstPage_Click(object sender, EventArgs e)
		{
			PageIndex = 0;
			BindSurvey();
		}

		protected void lbtnLastPage_Click(object sender, EventArgs e)
		{
			PageIndex = TotalPages - 1;
			BindSurvey();
		}

		protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.PageIndex = 0;
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindSurvey();

			var currentPageIndex = this.PageIndex;
			var totalPages = TotalPages;

			// set pager index links
			List<PagerIndex> indexs = new List<PagerIndex>();

			if(totalPages <= 25)
			{
				for(int i = 1; i <= totalPages; i++)
				{
					indexs.Add(new PagerIndex(i.ToString()));
				}
			}
			else
			{
				for(var i = 13; i > 0; i--)
				{
					if(indexs.Count >= 27)
					{
						break;
					}

					var index = currentPageIndex - i;

					if(index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}

				for(var i = 0; i <= 13; i++)
				{
					if(indexs.Count > 27)
					{
						break;
					}

					var index = currentPageIndex + i;

					if(index <= totalPages && index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}
			}

			this.RptrPageIndex.DataSource = indexs;
			this.RptrPageIndex.DataBind();

			this.LabelTotalPages.Text = totalPages.ToString();
			this.LabelTotalRecords.Text = TotalRecords.ToString();

			// disenabel the current pagerindex link
			if(RptrPageIndex.Items.Count > 0)
			{
				for(int i = 0; i < RptrPageIndex.Items.Count; i++)
				{
					LinkButton thePagerIndexLink = RptrPageIndex.Items[i].FindControl("LBPageIndex") as LinkButton;

					if(thePagerIndexLink.Text.Trim().Equals((currentPageIndex + 1).ToString()))
					{
						thePagerIndexLink.Enabled = false;
					}
				}
			}

			lbtnFirstPage.Enabled = (currentPageIndex > 0);

			lbtnLastPage.Enabled = totalPages > 1 && (currentPageIndex != totalPages - 1);
		}

		protected void rptSurvey_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if(e.CommandName == "delete")
			{
				var lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;
				var surveyID = int.Parse(lbtnDelete.CommandArgument);

				if(_SurveyProvider == null)
				{
					_SurveyProvider = new SurveyProvider();
				}

				if(_SurveyProvider.DeleteSurvey(surveyID))
				{
					//BindSurvey();
				}
			}
		}

		protected void rptSurvey_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var dataItem = e.Item.DataItem as Survey;

			if(dataItem != null)
			{
				var hlSurveyResult = e.Item.FindControl("hlSurveyResult") as HyperLink;
				var hlSurveyTitle = e.Item.FindControl("hlSurveyTitle") as HyperLink;
				var hlSurveyEdit = e.Item.FindControl("hlSurveyEdit") as HyperLink;
				var lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;

				hlSurveyResult.NavigateUrl = "~/SurveyResult.aspx?surveyid=" + dataItem.SurveyID.ToString();
				hlSurveyTitle.Text = dataItem.SurveyTitle;
				hlSurveyTitle.NavigateUrl = "~/SurveyDetail.aspx?surveyid=" + dataItem.SurveyID.ToString();
				hlSurveyEdit.NavigateUrl = "~/SurveyEdit.aspx?surveyid=" + dataItem.SurveyID.ToString();
				lbtnDelete.CommandArgument = dataItem.SurveyID.ToString();

				hlSurveyEdit.Visible = false;
				lbtnDelete.Visible = false;

				var user = SiteContext.Current.CurrentUser;

				if(user != null && user.Role != null)
				{
					lbtnDelete.Visible = user.Role.RoleName.ToLower().Equals("storeadmin") || user.Role.RoleName.ToLower().Equals("storemanager") || user.Role.RoleName.ToLower().Equals("admin");
					hlSurveyEdit.Visible = user.Role.RoleName.ToLower().Equals("storeadmin") || user.Role.RoleName.ToLower().Equals("storemanager") || user.Role.RoleName.ToLower().Equals("admin");
				}
			}
		}

		private void BindSurvey()
		{
			var data = this.DataSourse;

			rptSurvey.DataSource = data;
			rptSurvey.DataBind();
		}
	}
}